package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.crm.entity.Gweq023Export;
import cn.e4j.modular.crm.service.Gweq023Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 销售订单查询
 * @author liuw
 * @date 2019/09/04
 */
@Controller
@RequestMapping("/crm/gweq023")
public class Gweq023Controller {

    @Autowired
    private Gweq023Service gweq023Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("crm:gweq023:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gweq023.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gweq023:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq023Service.getList(page, inDto);
        return new PageReData<>(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method= RequestMethod.GET)
    @RequiresPermissions("crm:gweq023:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response,@DtoParam DtoWapper dtoWapper){
        List<Dto> list = gweq023Service.getList(null, dtoWapper.getDto());
        JSONArray json_arr = new JSONArray();
        for (Map<String, Object> map1 : list) {
            JSONObject json_obj = new JSONObject();
            for (Map.Entry<String, Object> entry : map1.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                json_obj.put(key, value);
            }
            json_arr.add(json_obj);
        }
        List<Gweq023Export> gweq023Exports = new ArrayList<>();
        JSONObject.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";//设置日期格式
        String str = JSONObject.toJSONString(json_arr, SerializerFeature.WriteDateUseDateFormat);
        gweq023Exports = JSONObject.parseArray(str, Gweq023Export.class);

        // 设置Excel相关参数
        ExportParams params = new ExportParams(null, "销售订单", ExcelType.XSSF);
        map.put(NormalExcelConstants.FILE_NAME, "销售订单");
        map.put(NormalExcelConstants.DATA_LIST, gweq023Exports);
        map.put(NormalExcelConstants.CLASS, Gweq023Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }


    /**
     * 查询出货数据
     */
    @RequestMapping("/getGwecData")
    @RequiresPermissions("crm:gweq023:ship")
    @ResponseBody
    public ResponseData getGwecData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq023Service.getGwecPage(page, inDto);
        return new PageReData<>(page, result);
    }

    /**
     * 查询验收数据
     */
    @RequestMapping("/getGwedData")
    @RequiresPermissions("crm:gweq023:check_accept")
    @ResponseBody
    public ResponseData getGwedData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq023Service.getGwedPage(page, inDto);
        return new PageReData<>(page, result);
    }

    /**
     * 查询开票数据
     */
    @RequestMapping("/getGwefData")
    @RequiresPermissions("crm:gweq023:invoice")
    @ResponseBody
    public ResponseData getGwefData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq023Service.getGwefPage(page, inDto);
        return new PageReData<>(page, result);
    }

    /**
     * 查询回款数据(废弃)
     */
    @RequestMapping("/getGweoData")
    @RequiresPermissions("crm:gweq023:payback")
    @ResponseBody
    public ResponseData getGweoData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq023Service.getGweoPage(page, inDto);
        return new PageReData<>(page, result);
    }

    /**
     * 查询回款数据
     */
    @RequestMapping("/getGweqData")
    @RequiresPermissions("crm:gweq023:payback")
    @ResponseBody
    public ResponseData getGweqData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gweq023Service.getGweqPage(page, inDto);
        return new PageReData<>(page, result);
    }
}
